package com.carpedil.wst.service;

import org.springframework.stereotype.Service;

import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/** @author Administrator */
@Service
@ServerEndpoint("/socketService")
public class MessagingSocketService {
  @OnOpen
  public void onOpen(Session session) throws IOException {
    //    session.getBasicRemote().sendText("Server connected successfully !");
    System.out.println("connected------------!!!");
  }

  @OnMessage
  public void getMessages(String message, Session session) throws IOException {
    System.out.println("messaging from client : " + message);
    String[] msg =
        new String[] {
          "年", "年", "有", "风", ",", "风", "吹", "年", "年", ",", "慢", "慢", "即", "漫", "漫", "!"
        };
    for (String s : msg) {
      try {
        Thread.sleep(10);
        session.getBasicRemote().sendText(s);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}
